/*
 * File: DataTypeSerializer.java
 *
 * a serialising helper class for the EfficientSoap data TYPEs,
 * using the EfficientSoap.org SOAP client library project.
 *
 * @author Christian Rau
 */

package org.efficientsoap.test.serializer;

import android.util.Log;
import java.io.IOException;
import java.util.List;
import org.efficientsoap.client.*;
import org.efficientsoap.test.datatypes.*;

/**
 * a serialising helper class for the EfficientSoap data TYPEs , using the
 * EfficientSoap.org SOAP client library project.
 * 
 * 
 * @author Christian Rau
 */
public class DataTypeSerializer {

	private static final String TAG = DataTypeSerializer.class.getSimpleName();

	private static final String NAMESPACE = "http://efficientsoap.org/test/datatypes";
	

	public DataTypeSerializer() {
		super();
	}

	public static void serializeComplexData(
			EfficientSoapSerializer<?> eSoapSerializer,
			ComplexData complexData, String tag)
			throws IllegalArgumentException, IllegalStateException, IOException {
		Log.d(TAG, "serializeComplexData()");
		eSoapSerializer.getSerializer().startTag(NAMESPACE, tag);
		if (complexData.getPrimitiveData() != null) {
			serializePrimitiveData(eSoapSerializer,
					complexData.getPrimitiveData(), "primitiveData");
		}
		if (complexData.getSimpleData() != null) {
			serializeSimpleData(eSoapSerializer, complexData.getSimpleData(),
					"simpleData");
		}
		if (complexData.getSimpleList() != null) {
			serializeSimpleDataList(eSoapSerializer,
					complexData.getSimpleList(), "simpleList");
		}
		eSoapSerializer.getSerializer().endTag(NAMESPACE, tag);
	}

	public static void serializePrimitiveData(
			EfficientSoapSerializer<?> eSoapSerializer,
			PrimitiveData primitiveData, String tag)
			throws IllegalArgumentException, IllegalStateException, IOException {
		Log.d(TAG, "serializePrimitiveData()");
		eSoapSerializer.getSerializer().startTag(NAMESPACE, tag);
		eSoapSerializer.serializeIntegerElem(primitiveData.getIntegerValue(),
				"integerValue");
		eSoapSerializer.serializeAmountElem(primitiveData.getLongValue(),
				"longValue");
		eSoapSerializer.serializeDoubleElem(primitiveData.getDoubleValue(),
				"doubleValue");
		eSoapSerializer.serializeBooleanElem(primitiveData.isBooleanValue(),
				"booleanValue");
		eSoapSerializer.getSerializer().endTag(NAMESPACE, tag);
	}

	public static void serializeRecursiveData(
			EfficientSoapSerializer<?> eSoapSerializer,
			RecursiveData recursiveData, String tag)
			throws IllegalArgumentException, IllegalStateException, IOException {
		Log.d(TAG, "serializeRecursiveData()");
		eSoapSerializer.getSerializer().startTag(NAMESPACE, tag);
		if (recursiveData.getComplexData() != null) {
			serializeComplexData(eSoapSerializer,
					recursiveData.getComplexData(), "complexData");
		}
		if (recursiveData.getComplexList() != null) {
			serializeComplexDataList(eSoapSerializer,
					recursiveData.getComplexList(), "complexList");
		}
		if (recursiveData.getRecursiveData() != null) {
			serializeRecursiveData(eSoapSerializer,
					recursiveData.getRecursiveData(), "recursiveData");
		}
		eSoapSerializer.getSerializer().endTag(NAMESPACE, tag);
	}

	public static void serializeSimpleData(
			EfficientSoapSerializer<?> eSoapSerializer, SimpleData simpleData,
			String tag) throws IllegalArgumentException, IllegalStateException,
			IOException {
		Log.d(TAG, "serializeSimpleData()");
		eSoapSerializer.getSerializer().startTag(NAMESPACE, tag);
		eSoapSerializer.serializeStringElem(simpleData.getStringValue(),
				"stringValue");
		if (simpleData.getIntegerList() != null) {
			eSoapSerializer.serializeIntegerList(simpleData.getIntegerList(),
					"integerList");
		}
		if (simpleData.getPrimitiveData() != null) {
			serializePrimitiveData(eSoapSerializer,
					simpleData.getPrimitiveData(), "primitiveData");
		}
		eSoapSerializer.getSerializer().endTag(NAMESPACE, tag);
	}

	public static void serializeAmountList(
			EfficientSoapSerializer<?> eSoapSerializer, List<Long> longList,
			String listElementTag) throws IllegalArgumentException,
			IllegalStateException, IOException, IllegalStateException,
			IOException {
		Log.d(TAG, "serializeAmountList()");
		if (null != listElementTag) {
			for (Long elem : longList) {
				eSoapSerializer.getSerializer().startTag(NAMESPACE,
						listElementTag);
				eSoapSerializer.serializeAmountElem(elem, listElementTag);
				eSoapSerializer.getSerializer().endTag(NAMESPACE,
						listElementTag);
			}
		} else {
			for (Long elem : longList) {
				eSoapSerializer.serializeAmountElem(elem, listElementTag);
			}
		}
	}

	public static void serializeBooleanList(
			EfficientSoapSerializer<?> eSoapSerializer,
			List<Boolean> booleanList, String listElementTag)
			throws IllegalArgumentException, IllegalStateException,
			IOException, IllegalStateException, IOException {
		Log.d(TAG, "serializeBooleanList()");
		if (null != listElementTag) {
			for (Boolean elem : booleanList) {
				eSoapSerializer.getSerializer().startTag(NAMESPACE,
						listElementTag);
				eSoapSerializer.serializeBooleanElem(elem, listElementTag);
				eSoapSerializer.getSerializer().endTag(NAMESPACE,
						listElementTag);
			}
		} else {
			for (Boolean elem : booleanList) {
				eSoapSerializer.serializeBooleanElem(elem, listElementTag);
			}
		}
	}

	public static void serializeComplexDataList(
			EfficientSoapSerializer<?> eSoapSerializer,
			List<ComplexData> complexDataList, String listElementTag)
			throws IllegalArgumentException, IllegalStateException,
			IOException, IllegalStateException, IOException {
		Log.d(TAG, "serializeComplexDataList()");
		if (null != listElementTag) {
			for (ComplexData elem : complexDataList) {
				eSoapSerializer.getSerializer().startTag(NAMESPACE,
						listElementTag);
				serializeComplexData(eSoapSerializer, elem, listElementTag);
				eSoapSerializer.getSerializer().endTag(NAMESPACE,
						listElementTag);
			}
		} else {
			for (ComplexData elem : complexDataList) {
				serializeComplexData(eSoapSerializer, elem, listElementTag);
			}
		}
	}

	public static void serializeIntegerList(
			EfficientSoapSerializer<?> eSoapSerializer,
			List<Integer> integerList, String listElementTag)
			throws IllegalArgumentException, IllegalStateException,
			IOException, IllegalStateException, IOException {
		Log.d(TAG, "serializeIntegerList()");
		if (null != listElementTag) {
			for (Integer elem : integerList) {
				eSoapSerializer.getSerializer().startTag(NAMESPACE,
						listElementTag);
				eSoapSerializer.serializeIntegerElem(elem, listElementTag);
				eSoapSerializer.getSerializer().endTag(NAMESPACE,
						listElementTag);
			}
		} else {
			for (Integer elem : integerList) {
				eSoapSerializer.serializeIntegerElem(elem, listElementTag);
			}
		}
	}

	public static void serializePrimitiveDataList(
			EfficientSoapSerializer<?> eSoapSerializer,
			List<PrimitiveData> primitiveDataList, String listElementTag)
			throws IllegalArgumentException, IllegalStateException,
			IOException, IllegalStateException, IOException {
		Log.d(TAG, "serializePrimitiveDataList()");
		if (null != listElementTag) {
			for (PrimitiveData elem : primitiveDataList) {
				eSoapSerializer.getSerializer().startTag(NAMESPACE,
						listElementTag);
				serializePrimitiveData(eSoapSerializer, elem, listElementTag);
				eSoapSerializer.getSerializer().endTag(NAMESPACE,
						listElementTag);
			}
		} else {
			for (PrimitiveData elem : primitiveDataList) {
				serializePrimitiveData(eSoapSerializer, elem, listElementTag);
			}
		}
	}

	public static void serializeSimpleDataList(
			EfficientSoapSerializer<?> eSoapSerializer,
			List<SimpleData> simpleDataList, String listElementTag)
			throws IllegalArgumentException, IllegalStateException,
			IOException, IllegalStateException, IOException {
		Log.d(TAG, "serializeSimpleDataList()");
		if (null != listElementTag) {
			for (SimpleData elem : simpleDataList) {
				eSoapSerializer.getSerializer().startTag(NAMESPACE,
						listElementTag);
				serializeSimpleData(eSoapSerializer, elem, listElementTag);
				eSoapSerializer.getSerializer().endTag(NAMESPACE,
						listElementTag);
			}
		} else {
			for (SimpleData elem : simpleDataList) {
				serializeSimpleData(eSoapSerializer, elem, listElementTag);
			}
		}
	}

}
